package cn.com.dhcc.turbo.dao.registry;


import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import cn.com.dhcc.app.core.CM;
import cn.com.dhcc.app.core.dao.AbstractCrudDao;
import cn.com.dhcc.app.core.exception.DaoException;
import cn.com.dhcc.app.core.handler.modulehandler.ModuleFeatureBeanRowWapper;
import cn.com.dhcc.app.core.handler.modulehandler.WhereCondition;
import cn.com.dhcc.app.core.handler.pagerhandler.Pager;
import cn.com.dhcc.app.core.handler.pagerhandler.PagerData;
import cn.com.dhcc.app.core.service.Log;
import cn.com.dhcc.app.core.service.Log.LOGTYPE;
import cn.com.dhcc.turbo.entity.registry.AdapterInfo;
/**
 *  适配器注册
 * 
 * @日期：2014-10-20  
 * @作者：古玉龙
 * @版权所有：东华软件
 * @版本：1.0
 */
@Repository
public class AdapterInfoDao extends AbstractCrudDao<AdapterInfo> {
	@SuppressWarnings("deprecation")
	private static Logger logger = Log.getExistLogger(LOGTYPE.TURBO);
	
	/**
	 * 获取本节点适配器信息
	 * @return
	 * @throws DaoException
	 */
	public PagerData<AdapterInfo> getLocalAdapterInfo(Pager pager,WhereCondition condition) throws DaoException {
		String msg = "";
	
		try {
			String sql = "select  a.id ,b.NODE_NAME,a.NODE_CODE,a.ADAPTER_CODE,a.ADAPTER_NAME,a.ADAPTER_TYPE,a.USE_SCENE,a.STATUS,a.WSDL_URL,a.auth_code,a.REMARK,a.MTIME " +
					" from  " + SI_TABLE("TURBO_ADAPTER_INFO")+" a  ,"+SI_TABLE("TURBO_NODE_INFO")+"  b  where a.NODE_CODE=b.NODE_CODE  ";
			return CM.getDao().getPagerList(" 获取本节点适配器信息", sql,new ModuleFeatureBeanRowWapper<AdapterInfo>(AdapterInfo.class), pager, condition,new Object[]{});
			 
		} catch (DaoException e) {
			msg = " 获取本节点适配器信息失败";
			throw new DaoException(msg, e);
		}
	}
	
	/**
	 * 获取本节点信息
	 * @return
	 * @throws DaoException
	 */
	public String localCode() throws DaoException {
		try {
			final String sql = "select " + " node_code as nodeCode" + " from " + SI_TABLE("TURBO_NODE_INFO")+" where islocal='1'";
			return  CM.getDao().queryForString("获取节点编码", sql,new Object[] {  }) ;
		} catch (DaoException e) {
			throw new DaoException("获取节点编码失败！", e);
		}
	}
	
	/**
	 * 获取适配器编码最大信息
	 * @return
	 * @throws DaoException
	 */
	public int localMaxCode(String localNodeCode) throws DaoException {
		try {
			final String sql = "select max(substr(ADAPTER_CODE,10))   from " + SI_TABLE("TURBO_ADAPTER_INFO") +" where node_code = ?";
			int maxCode =(CM.getDao().queryForInt("获取最大节点编码", sql, new Object[]{localNodeCode}) != null ? CM.getDao().queryForInt("获取最大节点编码", sql, new Object[]{localNodeCode}):0)+1;
			return  maxCode ;
		} catch (DaoException e) {
			throw new DaoException("获取本节点最大编码失败！", e);
		}
	}
    
	public String getNoDeName(String nodeCode) throws DaoException{ 
		try { 
			String sql="select"+"  DISTINCT  b.NODE_NAME as noDeName "+"from "+  SI_TABLE("TURBO_ADAPTER_INFO")+"  a,"+SI_TABLE(" TURBO_NODE_INFO")+"  b  where a.NODE_CODE=b.NODE_CODE and a.NODE_CODE=?";
	        return CM.getDao().queryForString("获取本节点名", sql, new Object[]{nodeCode});
		} catch (DaoException e) {
			throw new DaoException("获取本节点名失败！", e);
		}
	}
	
	/*
	 * 获取本地节点列表
	 * **/
	public List<AdapterInfo> getLocalName()  throws DaoException {
		try {
			String sql = "select     b.NODE_NAME  as noDeName,a.NODE_CODE  from "+SI_TABLE("TURBO_ADAPTER_INFO")+("  a ,")+SI_TABLE("TURBO_NODE_INFO")+"  b  where   A .NODE_CODE = b.NODE_CODE";
			return CM.getDao().listModule("获取本地节点数据", sql,AdapterInfo.class,new Object[]{});
		} catch (DaoException e) {
			throw new DaoException("获取本地节点数据失败", e);
		}
	}
	
	/**
	 * 根据节点编码，系统编码，适配器编码获取对应的适配器信息
	 * @param nodeCode
	 * @param adapterCode
	 * @return
	 * @throws DaoException 
	 */
	public AdapterInfo getAdapterInfo(String nodeCode,String adapterCode) throws DaoException{
		
		
		String sql=null;
		try {
			
			sql="select * from "+SI_TABLE("turbo_adapter_info")+" a " +
			   " where a.NODE_CODE=? and  a.ADAPTER_CODE=?";
			return CM.getDao().getModule("获取适配器信息", sql, AdapterInfo.class, new Object[]{nodeCode,adapterCode});
		} catch (DaoException e) {
			String msg = "获取适配器信息失败";
			logger.error(msg, e);
			throw new DaoException("通过适配器id返回与其相关的系统ID失败！", e);
		}
		
	}
/**
 * 本地适配器
 * @return
 * @throws DaoException
 */
	public List<AdapterInfo> getLocalAdapterList() throws DaoException {
		try {
			final String sql = "select *   from " + SI_TABLE("turbo_adapter_info") +" where islocal=1 and wsdl_url is not null";
			return CM.getDao().list("获取本地适配器", sql,new ModuleFeatureBeanRowWapper<AdapterInfo>(AdapterInfo.class),new Object[]{});
		} catch (DaoException e) {
			throw new DaoException("获取本地适配器失败！", e);
		}
	}
	/**
	 *  交换节点适配器
	 * @return
	 * @throws DaoException
	 */
	public List<AdapterInfo> getRelNodeAdapterList() throws DaoException {
		try {
			final String sql = "select *   from " + SI_TABLE("turbo_adapter_info") +" where islocal=0 and wsdl_url is not null";
			return CM.getDao().list("获取本地适配器", sql,new ModuleFeatureBeanRowWapper<AdapterInfo>(AdapterInfo.class),new Object[]{});
		} catch (DaoException e) {
			throw new DaoException("获取本地适配器失败！", e);
		}
	}
}
